﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Diagnostics;

namespace WindowsGame1.GameObjects
{
    public class GameObject : ISizable, ICanCollide, IRenderable,IMovable
    {
        Texture2D _texture;
        Rectangle _textureSprite;
        Vector2 _location;

        /// <summary>
        /// Direction and speed across X/Y
        /// </summary>
        public Vector2 Vector { get; set; }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="texture">a texture that contains this object and may contain other objects</param>
        /// <param name="objectTextureLocation">where in the texture is this object</param>
        public GameObject(Microsoft.Xna.Framework.Graphics.Texture2D texture, Rectangle textureSprite)
        {
            _texture = texture;
            _textureSprite = textureSprite;
            
            
        }
        public bool IntersectsY(float y)
        {

            return IntersectsY(y, 1);
        }
        public bool IntersectsY(float y,int height)
        {
            return this.Position.Intersects(new Rectangle(this.Position.X, (int) y, this.Position.Width, height));
        }

        #region ISizable Members

        int? _height;
        int? _width;
        /// <summary>
        /// assumes that objects MUST have a height
        /// </summary>
        public int Height
        {
            get { return _height ?? _textureSprite.Height; }
            set
            {
                _height = value;
            }
        }

        public int Width
        {
            get
            {
                return _width ?? _textureSprite.Width;
            }
            set
            {
                _width = value;
            }
        }

        #endregion

        #region ICanCollide Members

        public void Update(GameTime gameTime)
        {
            Debug.Assert(Vector.X != 0);
            Debug.Assert(Vector.Y != 0);

            float moveFactor = (float) gameTime.ElapsedGameTime.TotalMilliseconds/1000.0f;
            float moveFactorX = Vector.X*moveFactor;
            float moveFactorY = Vector.Y*moveFactor;

            _location.X +=moveFactorX ;
            _location.Y += moveFactorY;
        }

        public Rectangle Position
        {
            get { return new Rectangle((int)_location.X,(int)_location.Y,Width,Height); }
        }

        #endregion

        #region IRenderable Members

        public Texture2D Texture
        {
            get {return _texture; }
        }

        public Rectangle PositionOnTexture
        {
            get {return _textureSprite; }
        }

        #endregion

        #region IMovable Members

        public void MoveTo(int x, int y)
        {
            _location.X = x;
            _location.Y = y;
        }

        #endregion

        #region IMovable Members


        public void SwitchDirectionX()
        {
            Vector= new Vector2(Vector.X*-1,Vector.Y);
        }

        public void SwitchDirectionY()
        {
            Vector = new Vector2(Vector.X,Vector.Y * -1);
        }

        #endregion


        #region ICanCollide Members


        public bool IsColliding(Rectangle rect)
        {
            return Position.Intersects(rect);
        }

        #endregion
    }
}
